package com.gmics.unifiq.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.gmics.android.R;
import com.gmics.unifiq.controller.EntityController;
import com.gmics.unifiq.entity.Event;
import com.gmics.unifiq.entity.Group;
import com.gmics.unifiq.entity.Notification;
import com.gmics.unifiq.entity.OpenEvent;

public class NotificationAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<Notification> nArr;


	public NotificationAdapter(Context context, ArrayList<Notification> nArr) {
		this.context = context;
		this.nArr = nArr;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return nArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return nArr.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return nArr.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View listView = new View(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listView = inflater.inflate(R.layout.notification_list_item, null);
		
		ImageView img = (ImageView) listView
				.findViewById(R.id.event_grp_img);
		TextView title = (TextView) listView.findViewById(R.id.title_tv);
		TextView update = (TextView) listView.findViewById(R.id.update_tv);
		TextView desc = (TextView) listView.findViewById(R.id.desc_tv);
		
		boolean notEvent = false;
		if (nArr.get(position).getName().equals("")){
			notEvent = true;
		}
		
		if (notEvent){
			title.setText(nArr.get(position).getOriginal());
			update.setText(nArr.get(position).getOriginal().split(" ")[0] + " on " + EntityController.convertDateTimeToString(nArr.get(position).getCreationDate()));
			desc.setText(nArr.get(position).getUpdate());
		}
		else{
			title.setText(nArr.get(position).getName());
			update.setText("Updated on " + EntityController.convertDateTimeToString(nArr.get(position).getCreationDate()));
			desc.setText("Shifted from " + nArr.get(position).getOriginal() + " to " + nArr.get(position).getUpdate());
		}
		
		if (nArr.get(position).getOpenEventID() != 0){
			img.setBackgroundResource(R.drawable.university);
		}
		else if (nArr.get(position).getEventID() != 0){
			img.setBackgroundResource(R.drawable.community);
		}
		
		if (nArr.get(position).getOriginal().split(" ")[0].equals("Unlocked")){
			img.setBackgroundResource(R.drawable.unlock);
		}
		else if (nArr.get(position).getOriginal().split(" ")[0].equals("Obtained")){
			img.setBackgroundResource(R.drawable.trophy);
		}
		
		return listView;
	}

}